//
//  WebVC.swift
//  mmda
//
//  Created by 李陆涛 on 2018/4/24.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit
import WebKit

class WebVC: BaseVC {
    
    var webView: WKWebView!
    var url:String?
    
    var pageType:Int = 0// 1为协议
    var agreeBlock:(()->Void)?
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setup()
    }
    
    private func setup() {
        
        
        self.view.backgroundColor = UIColor.white
        let config = WKWebViewConfiguration()
        
        if pageType == 1 {
            
            webView = WKWebView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight - 44), configuration: config)
            
            let refuseBtn = UIButton(type: .custom)
            refuseBtn.setTitle("我拒绝", for: .normal)
            refuseBtn.setTitleColor(UIColor.lightGray, for: .normal)
            refuseBtn.titleLabel?.font = kFont14
            refuseBtn.addTarget(self, action: #selector(userRefuseAction), for: .touchUpInside)
            refuseBtn.frame = CGRect(x: 0, y: kScreenHeight - 44, width: kScreenWidth / 2, height: 44)
            
            self.view.addSubview(refuseBtn)
            
            
            let agreeBtn = UIButton(type: .custom)
            agreeBtn.setTitle("我同意", for: .normal)
            agreeBtn.setTitleColor(UIColor.red, for: .normal)
            agreeBtn.titleLabel?.font = kFont14
            agreeBtn.addTarget(self, action: #selector(userAgreementAction), for: .touchUpInside)
            agreeBtn.frame = CGRect(x: kScreenWidth / 2, y: kScreenHeight - 44, width: kScreenWidth / 2, height: 44)
            
            self.view.addSubview(agreeBtn)
            
            
            let lineView1 = UIView(frame: CGRect(x: 0, y: kScreenHeight - 44, width: kScreenWidth, height: 1))
            lineView1.backgroundColor = UIColor.groupTableViewBackground
            self.view.addSubview(lineView1)
            
            let lineView2 = UIView(frame: CGRect(x: kScreenWidth / 2, y: kScreenHeight - 44, width: 1, height: 44))
            lineView2.backgroundColor = UIColor.groupTableViewBackground
            self.view.addSubview(lineView2)
            
        } else {
            
            webView = WKWebView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight), configuration: config)
        }
        
        
        self.view.addSubview(webView)
        
        webView.uiDelegate = self
        webView.navigationDelegate = self
        
        
        guard let url = self.url else { return }
        
        let request = URLRequest(url: URL(string: url)!)
        
        self.view.makeToastActivity(.center)
        webView.load(request)
    }
    
    @objc private func userRefuseAction() {
        
        self.dismiss(animated: true, completion: nil)
    }
    
    @objc private func userAgreementAction() {
        
        self.agreeBlock?()
        self.dismiss(animated: false, completion: nil)
    }
}

extension WebVC: WKUIDelegate,WKNavigationDelegate {
    
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        
        webView.evaluateJavaScript("document.documentElement.style.webkitUserSelect='none'", completionHandler: nil)
        webView.evaluateJavaScript("document.documentElement.style.webkitTouchCallout='none'", completionHandler: nil)
    }
    
    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        
        self.view.hideToastActivity()
    }
    
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        
        self.view.hideToastActivity()
    }
    
}

